package com.sinheldrin.musicplayer.music
{
	import mx.collections.ArrayCollection;

	public class Playlist extends MusicCollection
	{
		public function Playlist()
		{
			super();
		}
		
		public function getRandomMusic():Music
		{
			var length:int = musics.length;
			if (length == 0)
				return null;
			var index:int = Math.floor(Math.random() * length);
			return Music(musics.getItemAt(index));
		}
		
		public function prevMusic(currentMusic:Music):Music
		{
			var length:int = musics.length;
			if (length == 0)
				return null;
			var index:int = 0;
			if (musics.contains(currentMusic) == true)
			{
				index = musics.getItemIndex(currentMusic) - 1;
				if (index < 0)
					index = length - 1;
			}
			var nextMusic:Music = musics.getItemAt(index) as Music;
			return nextMusic;
		}
		
		public function nextMusic(currentMusic:Music):Music
		{
			var length:int = musics.length;
			if (length == 0)
				return null;
			var index:int = 0;
			if (musics.contains(currentMusic) == true)
			{
				index = musics.getItemIndex(currentMusic) + 1;
				if (index == length)
					index = 0;
			}
			var nextMusic:Music = musics.getItemAt(index) as Music;
			return nextMusic;
		}
	}
}